package com.yh.patterns.singleton.singleton_Multiton;

import java.util.ArrayList;
import java.util.Random;

/**
 * @Auther:云尚学Java
 * 使用单例模式的思想实现多例模式，确保系统中某个类的对象只能存在有限个，如两个或三个，设计并编写代码实现一个多例类
 */
public class Multiton {
    //两个或者三个
    private static ArrayList<Multiton> multitonList = new ArrayList<>();
    private final static int NUMBER=3;
    //无参构造
    private Multiton(){}
    public static Multiton getInstance() {
        //创建实例
        if (multitonList.size()<NUMBER){
            System.out.println("创建新实例成功");
            Multiton current =  new Multiton();
            multitonList.add(current);
            return current;
        }else{
            System.out.println("不能创建更多的实例");
            return multitonList.get(new Random().nextInt(multitonList.size()));
        }
    }
}

